package com.kehao.leetcode.jzoffer;

import org.junit.Test;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/7/5 14:01
 */
public class JZSolution047 {
    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private boolean removeSubTree(TreeNode node){
        if(node==null){
            return true;
        }
        boolean leftResult = removeSubTree(node.left);
        boolean rightResult = removeSubTree(node.right);
        if(leftResult){
            node.left = null;
        }
        if(rightResult){
            node.right = null;
        }
        if(leftResult&&rightResult&&node.val==0){
            return true;
        }else {
            return false;
        }

    }

    public TreeNode pruneTree(TreeNode root) {
        boolean remove = removeSubTree(root);
        if(remove){
            return null;
        }
        return root;
    }

    @Test
    public void test01(){
        TreeNode root = new TreeNode(0);
        pruneTree(root);
        System.out.println(root.val);
    }
}
